package com.yeban.javase.day07.test3_stringbuilder;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/15
 *
 * //2.给定一个字符串数组。按照字典顺序进行从小到大的排序。{"nba","abc","cba","zz","qq","haha"}
 * 方式一：数组中元素，自身具备比较性
 * String implements Comparable<String>接口，重写了compareTo(o)
 * Arrays.sort(arr)
 *
 * 方式二：容器具备比较性  传入Comparator比较器
 * Arrays.sort(arr , new Comparator<String>(){ ... })
 */
public class Test02 {

    public static void main(String[] args) {
        String[] str1 = {"nba","abc","cba","zz","qq","haha"};
        String[] str2 = {"nba","abc","cba","zz","qq","haha"};

        // 方法一
        Arrays.sort(str1);
        System.out.print("方式一排序：");
        Arrays.stream(str1).forEach(System.out::print);

        // 方法二
        Arrays.sort(str2, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.print("\n方式二排序：");
        Arrays.stream(str2).forEach(System.out::print);

    }

}
